package edu.cs.usask.ca.silver.plugin.rcp.components;

import org.apache.log4j.Logger;

import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.businesslogic.ids.BaseId;
import edu.cs.usask.ca.silver.definitions.SystemProperties;
import edu.cs.usask.ca.silver.plugin.rcp.jobs.LoggerReportScheduler;

/**
 * Silver ID enums incapsulating icon path and so on.
 * 
 * @author Yudi Xue
 * 
 */
public enum SilverEntitiesIncludingIconsEnumerations {

	MODELPROJECT("edu.cs.usask.ca.silver.businesslogic.ids.ModelProjectID",
			SystemProperties.FILE_SEPARATOR + "icons" + SystemProperties.FILE_SEPARATOR + "entities"
					+ SystemProperties.FILE_SEPARATOR + "modelproject.gif"),

	MODELVERSION("edu.cs.usask.ca.silver.businesslogic.ids.ModelVersionID",
			SystemProperties.FILE_SEPARATOR + "icons" + SystemProperties.FILE_SEPARATOR + "entities"
					+ SystemProperties.FILE_SEPARATOR + "modelversion.gif"),

	SCENARIOCOLLECTION(
			"edu.cs.usask.ca.silver.businesslogic.ids.ScenarioCollectionID",
			SystemProperties.FILE_SEPARATOR + "icons" + SystemProperties.FILE_SEPARATOR + "entities"
					+ SystemProperties.FILE_SEPARATOR + "collection.png"),

	SCENARIO("edu.cs.usask.ca.silver.businesslogic.ids.ScenarioID", SystemProperties.FILE_SEPARATOR + "icons"
			+ SystemProperties.FILE_SEPARATOR + "entities"
			+ SystemProperties.FILE_SEPARATOR + "scenario.gif"),

	BASELINE_SCENARIO("edu.cs.usask.ca.silver.businesslogic.ids.ScenarioID",
			SystemProperties.FILE_SEPARATOR + "icons" + SystemProperties.FILE_SEPARATOR + "entities"
					+ SystemProperties.FILE_SEPARATOR + "baseline.gif"),

	USER("edu.cs.usask.ca.silver.businesslogic.ids.UserID", SystemProperties.FILE_SEPARATOR + "icons"
			+ SystemProperties.FILE_SEPARATOR + "user"
			+ SystemProperties.FILE_SEPARATOR + "user.png"),

	GROUP("edu.cs.usask.ca.silver.businesslogic.ids.GroupID", SystemProperties.FILE_SEPARATOR + "icons"
			+ SystemProperties.FILE_SEPARATOR + "group"
			+ SystemProperties.FILE_SEPARATOR + "group.png"),

	FILE("edu.cs.usask.ca.silver.businesslogic.ids.FileID", SystemProperties.FILE_SEPARATOR + "icons"
			+ SystemProperties.FILE_SEPARATOR + "group"
			+ SystemProperties.FILE_SEPARATOR + "group.png");

	private final String className;
	private final String iconPath;
	private static Logger logger = Logger.getLogger(SilverEntitiesIncludingIconsEnumerations.class);

	SilverEntitiesIncludingIconsEnumerations(String className, String iconPath) {
		this.className = className;
		this.iconPath = iconPath;
	}

	public String getClassName() {
		return this.className;
	}

	public String getIconPath() {
		return this.iconPath;
	}

	public static String getEnumIdNameBasedOnBaseIdClassName(BaseId baseId)
			throws Exception {
		String compare = baseId.getClass().getName();
		for (SilverEntitiesIncludingIconsEnumerations idEnum : SilverEntitiesIncludingIconsEnumerations
				.values())
			if (compare.compareTo(idEnum.className) == 0) {
				// TODO handle null pointer exception
				// if (compare.equals(SCENARIO.className)) {
				// if (SilverBusinessLogicController.borrowScenario()
				// .isBaseLineScenario((ScenarioID) baseId)) {
				// SilverBusinessLogicController
				// .returnLastScenarioObject();
				// return BASELINE_SCENARIO.name();
				// }
				// return SCENARIO.name();
				// }
				return idEnum.name();
			}
		return null;
	}

	public static String getNameOfTheBaseIdFromDatabase(BaseId baseId, StorageI storage) {
		try {
			return baseId.getEntryNameFromDatabase(storage);
		} catch (NullPointerException e1) {
			LoggerReportScheduler.scheduleErrorLogging(logger,"NullPointerException", e1);
		} catch (Exception e) {
			LoggerReportScheduler.scheduleErrorLogging(logger,"Exception", e);
		}
		return null;
	}

	public static String getEnumIdIconPathBasedOnBaseIdClassName(BaseId baseId)
			throws Exception {
		String compare = baseId.getClass().getName();
		for (SilverEntitiesIncludingIconsEnumerations idEnum : SilverEntitiesIncludingIconsEnumerations
				.values())
			if (compare.compareTo(idEnum.className) == 0) {
				// TODO handle null pointer exception
				// if (compare.equals(SCENARIO.className)) {
				// if (SilverBusinessLogicController.borrowScenario()
				// .isBaseLineScenario((ScenarioID) baseId)) {
				// SilverBusinessLogicController
				// .returnLastScenarioObject();
				// return BASELINE_SCENARIO.name();
				// }
				// return SCENARIO.name();
				// }
				return idEnum.getIconPath();
			}
		return null;
	}

}
